﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;

public partial class ProductDetail : System.Web.UI.Page
{
     SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ShopLaptopConnectionString"].ConnectionString);
        //displayMenu();
        displayProduct();
        if (Session["Customer"] != null)
        {
            txtContent.Visible = true;
            btnSend.Visible = true;
            //txtEmail.Visible = true;
        }
        else
        {
            //txtEmail.Visible = false;
            txtContent.Visible = false;
            btnSend.Visible = false;
        }
        DisplayComment();
    }

    protected void DisplayComment()
    {
        string proID = Request.QueryString["proID"];
        try
        {
            SqlCommand cmd = new SqlCommand("select top 20 * from Comment where proID =" + proID + "and status=1 order by DateSend desc ", conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dlCommnet.DataSource = reader;
            dlCommnet.DataBind();
            conn.Close();
        }
        catch (Exception)
        {
            
            //throw;
        }
        
    }
    

    protected void displayProduct()
    {
        try
        {
            string proID = Request.QueryString["proID"];
            
            SqlCommand cmd = new SqlCommand("Select * from product where proID=" + proID, conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.Read())
            {
                lbProName.Text = rd["ProName"].ToString();
                lbProDesc.Text = rd["ProDesc"].ToString();
                lbPrice.Text = rd["ProPrice"].ToString();
                imProduct.ImageUrl = "~/picture/" + rd["ProImage"].ToString();
            }
            conn.Close();
            
        }
        catch
        { }
    }
    protected void imCart_Click(object sender, ImageClickEventArgs e)
    {
        int proID = Convert.ToInt32(Request.QueryString["proID"]);
        
        ShopCart ca = new ShopCart();
        ca.ProID=proID;
        
        ArrayList listProID = new ArrayList();
        if (Session["proID"] == null)
        {
            ca.SoLuong = 1;
            listProID.Add(ca);
        }
        else
        {
            listProID.AddRange((ArrayList)Session["proID"]);
            bool check = true;
            foreach (object a in listProID)
            {
                ShopCart newCa = (ShopCart)a;
                if (newCa.ProID == proID)
                {
                    check = false;
                    newCa.SoLuong++;
                }
            }
            if (check)
            {
                ca.SoLuong = 1;
                listProID.Add(ca);
            }
        }
        Session["proID"] = listProID;
        Response.Redirect("Cart.aspx");
        //Response.Redirect("Cart.aspx");
    }

    public bool IsNumber(string pValue)
    {
        foreach (Char c in pValue)
        {
            if (!Char.IsDigit(c))
                return false;
        }
        return true;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtContent.Text == "")
        {
            ShowPopUpMsg("Content is not blank!");
            return;
        }
        Customer cus = (Customer)Session["Customer"];
        //string cusID = Session["Customer"].ToString();
        string proID = Request.QueryString["proID"];
        SqlCommand cmd = new SqlCommand(@"Insert into Comment values("+cus.CusID+","+proID+",'"+DateTime.Now+"','"
            +txtContent.Text+"','"+cus.CusEmail+"',1)",conn);
        conn.Open();
        int check = cmd.ExecuteNonQuery();
        conn.Close();
        DisplayComment();
    }

    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }
    protected void lbtnShopCart_Click(object sender, EventArgs e)
    {
        if (Session["proID"] != null)
        {
            Response.Redirect("Cart.aspx");
        }
        Response.Redirect("CartEmpty.aspx");
    }
}
